/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author HuuTai_it
 */
public class Cannon extends Sprite{
    public boolean fireable;
    public boolean firing;
    public int count;
    private int direction;
    public int xDes;
    public int yDes;
    private Ship owner;
    public Cannon(Ship s, Image image) throws Exception{
        super(image, 24,24);
        count = 0;
        fireable = true;
        firing = false;
        direction = 0;
        owner = s;
        this.defineCollisionRectangle(12, 12, 2, 2);
        setPosition(s.getX(), s.getY());
        setVisible(false);
    }
    public int setD(int d)
    {
        try
        {
        setVisible(true);
        firing  = true;
        direction = d;
        setFrame(0);
        setPosition(owner.getX() , owner.getY() );//?o dau ra the nay
        count  = -1;
        xDes = owner.getX()  + 3 * owner.dirc[0][direction - 1];
        yDes = owner.getY()  + 3 * owner.dirc[1][direction - 1];
        while(xDes < 0 || yDes < 0 || xDes > 216 || yDes > 216)
        {
            xDes -= owner.dirc[0][direction - 1];
            yDes -= owner.dirc[1][direction - 1];
        }
        int xTemp = xDes, yTemp = yDes;
        while(xTemp != owner.getX() || yTemp != owner.getY())
        {
            if(owner.gameMng.element[xTemp/24][yTemp/24] == 2)
            {
                xDes = xTemp - owner.dirc[0][direction - 1];
                yDes = yTemp - owner.dirc[1][direction - 1];
            }
            xTemp -= owner.dirc[0][direction - 1];
            yTemp -= owner.dirc[1][direction - 1];
            
        }
        if(xDes == owner.getX() && yDes == owner.getY())
        {
            firing = false;   
            setVisible(false);
            setPosition(owner.getX() , owner.getY() );
            count = -1 ;
            return 0;
        }
        }
            catch(ArrayIndexOutOfBoundsException ex)
            {
                ex.printStackTrace();
                System.out.println(ex);
            }
        return 2;
    }
    public void stopFire()
    {
        firing = false;   
        setVisible(false);
        setPosition(owner.getX() , owner.getY() );
        count = -1 ;
    }
    public int fire()
    {
        try
        {
            if(getX() == xDes && getY() == yDes)
            {
                if(count == -1) 
                {
                    count = 0;
                    setFrame(count + 1);
                }
                if(count < 24 && count != -1)
                {
                    count++;
                    nextFrame();
                }
                else
                {
                    firing = false;   
                    setVisible(false);
                    setPosition(owner.getX() , owner.getY() );
                    count = -1 ;
                    return 1;
                }//endmove//
            }
            else
            if(firing)       
            {        
                move(owner.dirc[0][direction - 1]/8,
                        owner.dirc[1][direction - 1]/8);
            }
        }
            catch(ArrayIndexOutOfBoundsException ex)
            {
                ex.printStackTrace();
                System.out.println(ex);
            }
        return 2;
    }
            
    
}
